﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.IO;

namespace ParkLBS.Controller
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               //string ss = weather_city("北京");
                string url = "http://weatherapi.market.xiaomi.com/wtr-v2/temp/realtime?cityId=101010100";
                string str = HttpUtility.GetData(url);
                int s1 = str.IndexOf("{",2);
                int s2 = str.LastIndexOf("}");

                string ss = str.Substring(s1, s2 - s1);
                XiaomiWeather info = new XiaomiWeather();
                info = JsonHelper.JsonDeserialize<XiaomiWeather>(ss);
                string url2 = "http://weatherapi.market.xiaomi.com/wtr/data/aqi?city_id=101010100";
                string str2 = HttpUtility.GetData(url2);
            }
        }

        public string weather_city(string city)
        {
            string temp = null;
            try
            {
                string strURL = "http://weather.news.sina.com.cn/cgi-bin/figureWeather/search.cgi";
                HttpWebRequest request;
                request = (HttpWebRequest)WebRequest.Create(strURL);
                request.Method = "POST"; //Post请求方式
                request.ContentType = "application/x-www-form-urlencoded"; //内容类型
                string paraUrlCoded = System.Web.HttpUtility.UrlEncode("city"); //参数经过URL编码
                paraUrlCoded = paraUrlCoded + "=" + System.Web.HttpUtility.UrlEncode(city, System.Text.Encoding.GetEncoding("GB2312"));
                byte[] payload;
                payload = System.Text.Encoding.GetEncoding("GB2312").GetBytes(paraUrlCoded); //将URL编码后的字符串转化为字节 
                request.ContentLength = payload.Length; //设置请求的ContentLength
                Stream writer = request.GetRequestStream(); //获得请求流
                writer.Write(payload, 0, payload.Length); //将请求参数写入流
                writer.Close(); //关闭请求流
                HttpWebResponse response;
                response = (HttpWebResponse)request.GetResponse(); //获得响应流
                Stream s;
                s = response.GetResponseStream();
                StreamReader objReader = new StreamReader(s, System.Text.Encoding.GetEncoding("GB2312"));
                string HTML = "";
                string sLine = "";
                int i = 0;
                while (sLine != null)
                {
                    i++;
                    sLine = objReader.ReadLine();
                    if (sLine != null)
                        HTML += sLine;
                }
                HTML = HTML.Replace("&lt;", "<");
                HTML = HTML.Replace("&gt;", ">");
                int start, stop;
                start = HTML.IndexOf("<img src=\"http://image2.sina.com.cn/dy/weather/images/figure/", 0, HTML.Length);
                stop = HTML.IndexOf("<td background=http://image2.sina.com.cn/dy/weather/images", start);
                temp = HTML.Substring(start, stop - start);
            }
            catch (Exception x)
            {
            }
            return temp;
        }





        /// <summary>
        ///  得到天气数据
        /// </summary>
        /// <returns>数组（0、天气；1、气温；2、风力；3、紫外线；4、空气）</returns>
        public static string[] GetWeather()
        {
            Regex regex;

            string[] weather = new string[5];
            string content = "";

            Match mcTmp;
            Match mcCity;
            int k = 1;

            HttpWebResponse theResponse;
            WebRequest theRequest;


            //ss1.htm 注意：ss1-ss303代表了不同的城市，是不连续的
            theRequest = WebRequest.Create("http://weather.news.qq.com/inc/ss1.htm");
            try
            {
                theResponse = (HttpWebResponse)theRequest.GetResponse();

                using (System.IO.Stream sm = theResponse.GetResponseStream())
                {
                    System.IO.StreamReader read = new System.IO.StreamReader(sm, Encoding.Default);
                    content = read.ReadToEnd();
                }
            }
            catch (Exception)
            {
                content = "";
            }


            string parttenTmp = "<td height=\"23\" width=\"117\" background=\"/images/r_tembg5.gif\" align=\"center\">(?<item1>[^<]+)</td>";
            k = 1;
            regex = new Regex(parttenTmp, RegexOptions.Compiled | RegexOptions.IgnoreCase);
            for (mcTmp = regex.Match(content), k = 1; mcTmp.Success; mcTmp = mcTmp.NextMatch(), k++)
            {

                weather[0] = mcTmp.Groups["item1"].Value;
            }
            parttenTmp = "height=\"23\" align=\"center\">(?<item1>[^/]+)</td>";
            k = 1;
            regex = new Regex(parttenTmp, RegexOptions.Compiled | RegexOptions.IgnoreCase);
            for (mcTmp = regex.Match(content), k = 1; mcTmp.Success; mcTmp = mcTmp.NextMatch(), k++)
            {
                weather[k] = mcTmp.Groups["item1"].Value;
            }
            return weather;
        }
    }
}